/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 2 -*- */
/**
* @file TestI2c.c
* 
* Function for testing the i2c library code.
* 
* @author Miguel Ángel Vílchez López 2010 <sacra@openmatica.es>
*
* Codigo is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* Codigo is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License along
* with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <i2c.cpp>

/**
* @example TestI2c.c
*/

int main(int argc, char *argv[])
{

	if(argc<2)
	{
		puts("\nUSE:\nTestI2C <type>\n");
		puts("types:");
		puts("\tsend <address> <data_to_send>");
		puts("\trecv <address> <command>");
		puts("\t  e.g.");
		puts("\t    TestI2C send 13 01 => Sends '48' and '49' to 0x13 I2C address.");
		puts("\t    TestI2C recv 13 1 => Sends the '49' to 0x13 I2C address and receive the response.\n");
		
		exit(EXIT_SUCCESS);
	}
	
	if (strcmp(argv[1],"recv") == 0)
	{
		CMI2c dispositivo("/dev/i2c-0",atoi(argv[2]),false);
		char *word=(char*)malloc(6 * sizeof(char));
		
		if (dispositivo.recvWord(word,atoi(argv[3])) == 0)
			printf("\nLo recibido es:\t%d\n",atoi(word));

		free(word);
	}
	
	else if (strcmp(argv[1],"send") == 0)
	{
		CMI2c dispositivo("/dev/i2c-0",atoi(argv[2]),false);
		if (dispositivo.sendBlock(reinterpret_cast<unsigned char*>(argv[3])) == 0)
			printf("\nSended!\n");
	}

}
